package com.esri.viewer
{
	import com.esri.ags.FeatureSet;
	import com.esri.ags.SpatialReference;
	import com.esri.ags.geometry.Geometry;
	import com.esri.ags.tasks.QueryTask;
	import com.esri.ags.tasks.supportClasses.Query;
	
	import comValue.*;
	
	import mx.controls.Alert;
	import mx.managers.CursorManager;
	import mx.rpc.AsyncResponder;
	import mx.rpc.Responder;
	import mx.rpc.events.*; 

	public class ArcGisQuery
	{
		private var pOutSpatialReference:SpatialReference;
		private var pWhereCase:String;
		private var pGeometry:Geometry;
		private var pSpatialRelationship:String;
		
		private var pQuery:Query;
		private var pQueryTask:QueryTask;
		private var infoKey:String;
		
		public function ArcGisQuery(value:String,s_infoKey:String)
		{
			infoKey=s_infoKey;
			
			pQuery=new Query();
			pQuery.returnGeometry=true;
			pQuery.outFields=["*"];
			
			pQueryTask=new QueryTask();
			pQueryTask.url=value;
			pQueryTask.showBusyCursor=true;

		}			
			//指定空间坐标系统
		  public	function set setOutSpatialReference(vaule:SpatialReference):void
		  {
			  this.pOutSpatialReference=vaule;
		  }
			//指定查询条件
		  public function set setWhereCase(value:String):void
		  {
			  this.pWhereCase=value;
		  }
		  //指定查询条件
		  public function get getWhereCase():String
		  {
			  return this.pWhereCase;
		  }
			//指定空间查询要素
		  public function set setGeometry(value:Geometry):void
		  {
		    this.pGeometry=value;
		  }
		  public function get getGeometry():Geometry
		  {
			  return this.pGeometry;
		  }
			//指定空间查询关系
		  public function set setSpatialRelationship(value:String):void
		  {
			  this.pSpatialRelationship=value;
		  }
			
			//执行查询操作
		  public function executeQuery():void
		  {
			  CursorManager.setBusyCursor();
			  	  
			  pQuery.outSpatialReference=this.pOutSpatialReference;
			  pQuery.where=this.pWhereCase;
			  pQuery.geometry=this.pGeometry;

			  if (this.pGeometry) 
				  pQuery.spatialRelationship=this.pSpatialRelationship;;
			
			  pQueryTask.execute(pQuery, new AsyncResponder(pQueryTask_executeCompleteHandler, pQueryTask_faultHandler));
		  }
		  //成功执行空间查询并返回相关结果
		  protected function pQueryTask_executeCompleteHandler(pFeatureSet:FeatureSet,token:Object = null):void
		  {
			  if(pFeatureSet.features.length<1)
			  {
				  Alert.show("没有查询到满足条件的要素！");
				  CursorManager.removeAllCursors();
				  
				  return;
			  }
			  			  
			  var infoData:Object={
				  key:infoKey,
				  value:pFeatureSet	
			  }	
				  
			  ViewerContainer.dispatchEvent(new AppEvent(AppEvent.DATA_ARCGISQUERY,infoData));		
		  }
		  protected function pQueryTask_faultHandler(info:Object, token:Object = null):void
		  {
			  ViewerContainer.dispatchEvent(new AppEvent(AppEvent.DATA_ARCGISQUERY,null));
			  CursorManager.removeAllCursors();
			  ComMethods.showErrorTips();
			  //mx.controls.Alert.show(info.toString());
		  }
	}
}